
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="建筑或子建筑名称，如“南食堂”“麻辣香锅档口”:" prop="name">
    <el-input v-model="formData.name" :clearable="true" placeholder="请输入建筑或子建筑名称，如“南食堂”“麻辣香锅档口”" />
</el-form-item>
        <el-form-item label="别名或简称:" prop="alias">
    <el-input v-model="formData.alias" :clearable="true" placeholder="请输入别名或简称" />
</el-form-item>
        <el-form-item label="节点类型（支持建筑/楼层/档口等）:" prop="type">
    <el-select v-model="formData.type" placeholder="请选择节点类型（支持建筑/楼层/档口等）" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in building_typeOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="父级节点ID，实现层级关系:" prop="parentId">
    <el-input v-model.number="formData.parentId" :clearable="true" placeholder="请输入父级节点ID，实现层级关系" />
</el-form-item>
        <el-form-item label="校区或分区名称:" prop="campusZone">
    <el-input v-model="formData.campusZone" :clearable="true" placeholder="请输入校区或分区名称" />
</el-form-item>
        <el-form-item label="楼层或区域名称，如“5楼”“二层”:" prop="floorLabel">
    <el-input v-model="formData.floorLabel" :clearable="true" placeholder="请输入楼层或区域名称，如“5楼”“二层”" />
</el-form-item>
        <el-form-item label="房间号（若有）:" prop="roomNumber">
    <el-input v-model="formData.roomNumber" :clearable="true" placeholder="请输入房间号（若有）" />
</el-form-item>
        <el-form-item label="区域/档口标识（如“东侧A区”）:" prop="areaLabel">
    <el-input v-model="formData.areaLabel" :clearable="true" placeholder="请输入区域/档口标识（如“东侧A区”）" />
</el-form-item>
        <el-form-item label="纬度（户外）:" prop="latitude">
    <el-input-number v-model="formData.latitude" style="width:100%" :precision="2" :clearable="true" />
</el-form-item>
        <el-form-item label="经度（户外）:" prop="longitude">
    <el-input-number v-model="formData.longitude" style="width:100%" :precision="2" :clearable="true" />
</el-form-item>
        <el-form-item label="入口数组，如 [{'name':'正门','lat':40.8261,'lng':114.8812}]:" prop="entrances">
    <ArrayCtrl v-model="formData.entrances" editable/>
</el-form-item>
        <el-form-item label="详细说明（用途、位置、营业时间等）:" prop="description">
    <el-input v-model="formData.description" :clearable="true" placeholder="请输入详细说明（用途、位置、营业时间等）" />
</el-form-item>
        <el-form-item label="图片数组，按顺序展示，如 ['img1.jpg','img2.jpg']:" prop="imageUrls">
    <SelectImage
     multiple
     v-model="formData.imageUrls"
     file-type="image"
     />
</el-form-item>
        <el-form-item label="负责人联系电话或档口电话:" prop="contactInfo">
    <el-input v-model="formData.contactInfo" :clearable="true" placeholder="请输入负责人联系电话或档口电话" />
</el-form-item>
        <el-form-item label="详细位置信息，如“南食堂二层东侧”:" prop="addressDetail">
    <el-input v-model="formData.addressDetail" :clearable="true" placeholder="请输入详细位置信息，如“南食堂二层东侧”" />
</el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createHbjgCampusLocation,
  updateHbjgCampusLocation,
  findHbjgCampusLocation
} from '@/api/app_core/hbjgCampusLocation'

defineOptions({
    name: 'HbjgCampusLocationForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'
// 图片选择组件
import SelectImage from '@/components/selectImage/selectImage.vue'
// 数组控制组件
import ArrayCtrl from '@/components/arrayCtrl/arrayCtrl.vue'


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const building_typeOptions = ref([])
const formData = ref({
            name: '',
            alias: '',
            type: '',
            parentId: undefined,
            campusZone: '',
            floorLabel: '',
            roomNumber: '',
            areaLabel: '',
            latitude: 0,
            longitude: 0,
            entrances: [],
            description: '',
            imageUrls: [],
            contactInfo: '',
            addressDetail: '',
        })
// 验证规则
const rule = reactive({
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findHbjgCampusLocation({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    building_typeOptions.value = await getDictFunc('building_type')
}

init()
// 保存按钮
const save = async() => {
      btnLoading.value = true
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return btnLoading.value = false
            let res
           switch (type.value) {
             case 'create':
               res = await createHbjgCampusLocation(formData.value)
               break
             case 'update':
               res = await updateHbjgCampusLocation(formData.value)
               break
             default:
               res = await createHbjgCampusLocation(formData.value)
               break
           }
           btnLoading.value = false
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
